package com.ciaojian.core.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.enums.business.CouponCodeOriginEnum;
import com.ciaojian.core.mapper.CouponCodeMapper;
import com.ciaojian.core.mapper.ProductGroupMapper;
import com.ciaojian.core.mapper.UserMemberMapper;
import com.ciaojian.core.mapper.UserTimesCardMapper;
import com.ciaojian.core.model.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;

/**
 * @author oxiny
 */
public abstract class ProductGroupService<M extends BaseMapper<T>, T> extends ServiceImpl<ProductGroupMapper, ProductGroup> {
    @Resource
    private UserMemberService<UserMemberMapper, UserMember> userMemberService;
    @Resource
    private UserTimesCardService<UserTimesCardMapper, UserTimesCard> userTimesCardService;
    @Autowired
    private CouponCodeService<CouponCodeMapper, CouponCode> couponCodeService;

    /**
     * 根据ID获取产品组合
     *
     * @param id ID
     * @return /
     */
    public Optional<ProductGroup> getById(Integer id) {
        return Optional.ofNullable(this.baseMapper.getById(id));
    }

    /**
     * 会员卡回调处理方法
     *
     * @param order    订单
     * @param memberId 会员卡ID
     */
    public void memberHandle(Order order, int memberId) {
        User user = new User().selectById(order.getUserId());
        userMemberService.openVip(user, memberId, 365);
    }

    /**
     * 次卡回调处理方法
     *
     * @param order       订单
     * @param timesCardId 次卡ID
     */
    public void timesCardHandle(Order order, int timesCardId) {
        // 查询用户的次卡信息
        UserTimesCard userTimesCard = userTimesCardService.getBaseMapper().getByUserIdAndCardId(order.getUserId(), timesCardId);
        // 是否可叠加
        TimesCard timesCard = new TimesCard().selectById(timesCardId);

        // 没有买过次卡
        if (userTimesCard == null) {
            userTimesCard = new UserTimesCard()
                    .setCardId(timesCardId)
                    .setOrderNo(order.getOrderNo())
                    .setDueDate(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, getExpiredDate(timesCard)))
                    .setUserId(order.getUserId());
            userTimesCard.insert();
        }
        // 买过次卡
        else {
            userTimesCard.setDueDate(DateUtil.offset(userTimesCard.getDueDate(), DateField.DAY_OF_YEAR, getExpiredDate(timesCard)));
            userTimesCard.updateById();
        }
    }

    /**
     * 获取到期天数
     *
     * @param timesCard 次卡信息
     * @return 到期天数
     */
    private Integer getExpiredDate(TimesCard timesCard) {
        if (timesCard.getExpiredType() == 0) {
            return timesCard.getDay();
        } else if (timesCard.getExpiredType() == 1) {
            return (int) DateUtil.betweenDay(new Date(), timesCard.getDueDate(), true);
        }
        return 0;
    }

    /**
     * 权益回调处理方法
     *
     * @param order          订单
     * @param memberDetailId 权益ID
     */
    public void memberDetailHandle(Order order, int memberDetailId, CouponCodeOriginEnum origin) {
        MemberDetail memberDetail = new MemberDetail().selectById(memberDetailId);
        couponCodeService.giveCouponCodeByMemberDetail(
                order.getUserId(),
                order,
                memberDetail,
                origin);
        // 绑定 order 的 coupon_id 为权益的id,以免API: /member-detail/user-bought查不出来
        order.setCouponId(memberDetailId);
    }

    /**
     * 卡包回调处理方法
     *
     * @param order           订单
     * @param couponPackageId 卡包ID
     */
    public void couponPackageHandle(Order order, Integer couponPackageId) {
        couponCodeService.giveCouponCodeByCouponPackage(order.getUserId(),
                order,
                couponPackageId,
                CouponCodeOriginEnum.BUY_PACKAGE);
        order.setCouponId(couponPackageId);
    }

}